<template>
    <div class="app-container">
        <div class="head">
            <img src="@/assets/img/Layout/position.png" />
            <p class="see-block">产品详情</p>
        </div>
        <div class="view-container">
            <el-row class="detail-item">
                <el-col :span="24">
                    <span>产品图片：</span>
                    <div style="width:100%;">
                        <template v-if="detailData.shareFileList.length > 0">
                            <div v-for="item in detailData.shareFileList" :key="item.id" class="item-img">
                                <img :src="item.fileUrl">
                            </div>
                        </template>
                        <template v-else>暂无</template>
                    </div>
                </el-col>
            </el-row>
            <el-row class="detail-item">
                <el-col :span="8">
                    <span>产品编码：</span>{{ detailData.specificationCode ?  detailData.specificationCode : '暂无'}}
                </el-col>
                <el-col :span="8">
                    <span>产品名称：</span>{{ detailData.specificationName ?  detailData.specificationName : '暂无'}}
                </el-col>
            </el-row>
            <el-row class="detail-item">
                <el-col :span="8">
                    <span>产品价格：</span>{{ detailData.sellingPrice !== null && detailData.sellingPrice !== '' ? detailData.sellingPrice : detailData.productPrice !== null && detailData.productPrice !== '' ? detailData.productPrice : '暂无价格' }}
                </el-col>
                <el-col :span="16">
                    <span>产品描述：</span>{{ detailData.description ?  detailData.description : '暂无'}}
                </el-col>
            </el-row>
            <el-row class="detail-item">
                <el-col :span="8">
                    <span>产品数量：</span>
                    <el-input-number v-model="detailData.num" :min="1"></el-input-number>
                </el-col>
            </el-row>
            <el-row class="detail-item">
                <el-col :span="16">
                    <span></span>
                    <el-button size="small" type="primary" class="btn-success" @click="buy(detailData, 'add')">加入购物车</el-button>
                    <el-button size="small" type="primary" class="btn-success" @click="buy(detailData, 'buy')">立即申请</el-button>
                </el-col>
            </el-row>
        </div>
    </div>
</template>

<script>
import { cartBuy, cartAdd } from "@/api/system/basedata/productCollection/productCollection"
import { detailConfig } from "@/api/system/setting/config/config"
export default {
    name: "list",
    data() {
        return {
            detailData: '',
            form: {
                purchaseOrderProductList:[]
            },
            config: '', // 1 销售订单 0 采购订单
        }
    },
    created() {
        this.detailData = this.$route.query.data
        this.getConfig()
    },
    methods: {
        // 获取参数配置详情
        getConfig(){
            detailConfig('generatePurchaseOrder', 'shoppingMall').then(res => {
                if(res.data.length > 0){
                    this.config = res.data[0].ifAllow
                }
            })
        },
        detailAll(){
            // if(this.detailData.description){
            //     window.open(this.detailData.description, '_blank')
            // }
        },
        // 立即购买
        buy(y, val){
            let obj = {
                productBasicId: y.productId,
                productBasicCode: y.productCode,
                productBasicName: y.productName,
                productId: y.specificationsId,
                productCode: y.specificationCode,
                productName: y.specificationName,
                specification: y.value1 == null ? '' : y.value1  + y.value2 == null ? '' : y.value2,
                num: y.num,
                untaxedAmount: y.sellingPrice !== null && y.sellingPrice !== '' ? y.sellingPrice : y.productPrice !== null && y.productPrice !== '' ? y.productPrice : 0,
                amount: Number(y.num) * Number(y.sellingPrice !== null && y.sellingPrice !== '' ? y.sellingPrice : y.productPrice !== null && y.productPrice !== '' ? y.productPrice : 0)
            }
            // console.log(obj)
            this.form.purchaseOrderProductList = []
            this.form.purchaseOrderProductList.push(obj)
            if(val == 'buy'){
                cartBuy(this.form).then(res => {
                    this.$modal.msgSuccess("操作成功!")
                    if(this.config == '0'){
                        this.$router.push({path:"/purchase/purchaseOrder/list"})
                    }else{
                        this.$router.push({path:"/sales/salesOrder/list"})
                    }
                })
            }else{
                cartAdd(obj).then(res => {
                    this.$modal.msgSuccess("添加成功!")
                })
            }
        },
    }
}
</script>

<style scoped>
::v-deep .el-input-number--medium{
    width: auto;
}
::v-deep .el-input-number--medium span{
    text-align: center;
}
.item-img{
  float: left;
  margin:0 10px 10px 0;
  width: 200px;
  height: 200px;
  overflow: hidden;
}
.item-img img{
  width: 100%;
}
</style>